
package com.be.beadmin.project.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.project.dto.input.PlanStageDto;
import com.be.beadmin.project.dto.output.PlanStageEx;
import com.be.beadmin.project.entity.PlanStage;
import com.be.beadmin.project.entity.PlanStage_;
import com.be.beadmin.project.mapper.PlanStageMapper;
import com.be.beadmin.project.mapper.PlanStageMapperEx;
import com.be.beadmin.project.repository.BatchPlanRepository;
import com.be.beadmin.project.repository.BatchProductRepository;
import com.be.beadmin.project.repository.PlanStageRepository;
import com.be.beadmin.project.service.PlanStageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-06-18
**/
@Service
@RequiredArgsConstructor
public class PlanStageServiceImpl implements PlanStageService {

    @Autowired
    private  PlanStageRepository planStageRepository;
    @Autowired
    private  PlanStageMapper planStageMapper;
    @Autowired
    private  PlanStageMapperEx planStageExMapper;
    @Autowired
    private BatchProductRepository batchProductRepository;
    @Override
    public Page<PlanStageEx> getPlanStageByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<PlanStage> page = planStageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(PlanStage_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(PlanStage_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<PlanStageEx> page1 = page.map(planStageExMapper::toEntity);
            return page1;

    }

    @Override
    public void addPlanStage(PlanStageDto planStageDto) {
            PlanStage result = planStageRepository.findPlanStageByIdAndDelete(planStageDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("PlanStage不存在,请重新查询");
            }
            PlanStage planStage = planStageMapper.toEntity(planStageDto);
            planStageRepository.save(planStage);
    }

    @Override
    public void updatePlanStage(PlanStageDto planStageDto) {
            PlanStage planStage = planStageRepository.findPlanStageById(planStageDto.getId());
            planStage = planStageMapper.toEntity(planStageDto);
            planStageRepository.save(planStage);
    }


    @Override
    public void delPlanStage(String id) {
        PlanStage planStage = planStageRepository.findPlanStageById(id);
        planStage.setDelete(1);
        planStageRepository.save(planStage);
    }

    @Override
    public void delPlanStages (List<String> ids) { }

    /**
     * 将planStageDto
     *
     * @param planStageDto
     * @return
     */
    @Override
    public PlanStage toEntity(PlanStageDto dto) {

        PlanStage planStage = planStageRepository.findPlanStageByIdAndDelete(dto.getId(), 0);


        if ( dto.getStageStartTime() != null ) {
            planStage.setStageStartTime( dto.getStageStartTime() );
        }
        if ( dto.getStageEndTime() != null ) {
            planStage.setStageEndTime( dto.getStageEndTime() );
        }
        if ( dto.getStagePlanStartTime() != null ) {
            planStage.setStagePlanStartTime( dto.getStagePlanStartTime() );
        }
        if ( dto.getStagePlanEndTime() != null ) {
            planStage.setStagePlanEndTime( dto.getStagePlanEndTime() );
        }

        if (planStage.getStageStartTime()!=null && planStage.getStageEndTime() != null) {
            planStage.setStageDuration((int) DateUtil.betweenDay(planStage.getStageStartTime(),planStage.getStageEndTime(),true));
        }

        return planStage;
    }

    /**
     * 根据柜体id和阶段名字获取到对应批次的name某个阶段的planstage
     *
     * @param batchProductId
     * @param name
     * @return
     */
    @Override
    public PlanStage getPlanStageByBatchProductIdAndName(String batchProductId, String name) {
        String planId = batchProductRepository.findBatchPlanByBatchProductId(batchProductId);
        PlanStage planStage = planStageRepository.findPlanStageByPlanIdAndStageNameAndDelete(planId, "生产装配", 0);
        return planStage;
    }
}